﻿using NHibernate.Linq;

using ZQNB.Common;
using ZQNB.Common.Encrypt;
using ZQNB.Common.NHExtensions;
using ZQNB.Entity.Accounts;
using ZQNB.Entity.Users;

namespace ZQNB.BaseLib.Users
{

    public class TestInitData
    {
        public static Teacher ExistTeacher = new Teacher()
        {
            LoginName ="exist",
            Nick = "exist",
            Name ="exist"

        };


        public static Account WhhcAdminAccount = new Account()
        {
            LoginName = "admin",
            CustomNo = "EEEE0001",
            Email = "admin@test.com",
            Password = "123456",
            PasswordFormat = PasswordFormat.Clear
        };

        public static Account ExistAccount = new Account()
        {
            LoginName = "exist",
            CustomNo = "exist",
            Email = "exist@exist.com",
            Password = "123456",
            PasswordFormat = PasswordFormat.Clear
        };

        public static Account Clone(Account account)
        {
            return new Account()
            {
                LoginName = account.LoginName,
                CustomNo = account.CustomNo,
                Email = account.Email,
                Password = account.Password,
                PasswordFormat = account.PasswordFormat
            };
        }
        public static T Clone<T>(T entity) where T : class ,new ()
        {
            T copy = new T();

            MyModelHelper.SetProperties<T>(copy, entity, new []{"Id"});

            return copy;
        }
    }


    public class MyUtHelper : SqlUtHelper
    {
        public override string GetConnStr()
        {
            return @"Data Source=.;Initial Catalog=NbV3DbTest;Persist Security Info=True;User ID=sa;Password=zqnb_123";
        }

        public override void InitData()
        {
            using (var session = OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    Account exist = TestInitData.Clone(TestInitData.ExistAccount);
                    session.Save(exist);

                    tx.Commit();
                }
            }
        }

        public override void ClearData()
        {
            using (var session = OpenSession())
            {
                //注意删除顺序，外键关联！！！
                using (var tx = session.BeginTransaction())
                {
                    var entities = session.Query<Teacher>();
                    foreach (var entity in entities)
                    {
                        session.Delete(entity);
                    }

                    tx.Commit();
                }
            }
        }
    }
}
